﻿package com.tvein.sanguo.swc.action.command
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.Slider;
	import fl.controls.dataGridClasses.*;
	import fl.controls.DataGrid;
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class Command23 extends MovieClip
	{
		public static const NAME:String = "Command-23";
		
		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var targetID:int;
		private var roleEnergy:int;
		private var roleEnergyAdd:int;
		private var roleMoney:int;
		
		private var skillID:int;
		private var skillRank:int;
		private var teacherID:int;
		private var lastTime:int = 0;
		
		private var teacherList:DataGrid;
		private var timeSlider:Slider;
		
		public function Command23()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtSkill.border = true;
			txtSkill.borderColor = 0x333333;
			
			txtMoney.border = true;
			txtMoney.borderColor = 0x333333;
			
			teacherList = new DataGrid();
			teacherList.x = 5;
			teacherList.y = 70;
			teacherList.width = 590;
			teacherList.height = 140;
			this.addChild(teacherList);
			
			timeSlider = new Slider();
			timeSlider.x = 250;
			timeSlider.y = 330;
			timeSlider.width = 240;
			timeSlider.height = 3;
			timeSlider.maximum = 100;
			timeSlider.minimum = 0;
			timeSlider.snapInterval = 10;
			timeSlider.tickInterval = 10;
			timeSlider.enabled = false;
			this.addChild(timeSlider);
			
			btnStart.visible = false;
			btnTimeMinus.visible = false;
			btnTimePlus.visible = false;
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnStart.addEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setTargetId":
					targetID = params.TargetId;
					break;
				case "setData":
					setData(params);
					reRoad();
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			var style:TextFormat = new TextFormat();
			style.color = 0xff6600;
			style.size = 16;
			style.font = "LiSu";
			
			teacherList.setRendererStyle("textFormat", style);
			teacherList.addEventListener(ListEvent.ITEM_CLICK, onSelected);
			
			var c0:DataGridColumn = new DataGridColumn("RoleID");
			var c1:DataGridColumn = new DataGridColumn("教师");
			var c2:DataGridColumn = new DataGridColumn("SkillID");
			var c3:DataGridColumn = new DataGridColumn("技能");
			var c4:DataGridColumn = new DataGridColumn("类别");
			var c5:DataGridColumn = new DataGridColumn("等级");
			var c6:DataGridColumn = new DataGridColumn("剩余小时");
			
			var type:String;
				
			c0.visible = false;
			c1.width = 190;
			c2.visible = false;
			c3.width = 100;
			c4.width = 100;
			c5.width = 100;
			c6.width = 100;
			
			teacherList.addColumn(c0);
			teacherList.addColumn(c1);
			teacherList.addColumn(c2);
			teacherList.addColumn(c3);
			teacherList.addColumn(c4);
			teacherList.addColumn(c5);
			teacherList.addColumn(c6);

			for (var i:int = 0; i < params.length; i++)
			{
				if (params[i].EnergyNum)
				{
					roleEnergy = params[i].EnergyNum;
				}
				
				if (params[i].EnergyAdd)
				{
					roleEnergyAdd = params[i].EnergyAdd;
				}
				
				if (params[i].RoleMoney)
				{
					roleMoney = params[i].RoleMoney;
				}
				
				if (params[i].SkillType)
				{
					switch (int(params[i].SkillType))
					{
						case 0:
							type = "";
							break;
						case 1:
							type = "<生产类>";
							break;
						case 2:
							type = "<经营类>";
							break;
						case 3:
							type = "<内政类>";
							break;
						case 4:
							type = "<军事类>";
							break;
						case 5:
							type = "<舌战类>";
							break;
						case 6:
							type = "<单挑类>";
							break;
						case 7:
							type = "<计略类>";
							break;
						case 8:
							type = "<战法类>";
							break;
						case 9:
							type = "<称号类>";
							break;
					}

					var year:int = int(params[i].EndTime.substr(0, 4));
					var month:int = int(params[i].EndTime.substr(5, 2)) - 1;
					var date:int = int(params[i].EndTime.substr(8, 2));
					var hours:int = int(params[i].EndTime.substr(11, 2));
					var minutes:int = int(params[i].EndTime.substr(14, 2));
					var seconds:int = int(params[i].EndTime.substr(17, 2));

					var nowTime:Date = new Date();
					var endTime:Date = new Date(year, month, date, hours, minutes, seconds);
					
					var jobTime:Number = (endTime.getTime() - nowTime.getTime()) / (1000 * 60 * 60);
					
					if (endTime > nowTime)
					{
						if (jobTime >= 1)
						{
							teacherList.addItem({"RoleID":params[i].RoleID, "教师":params[i].RoleName, "SkillID":params[i].SkillID, "技能":params[i].SkillName, "类别":type, "等级":params[i].SkillRank, "剩余小时":Math.floor(jobTime)});
						}
					}
				}
			}
		}
		
		private function reRoad():void
		{
			if (timeSlider.value == 0)
			{
				txtTime.text = "";
				txtMoney.text = "";
				btnTimeMinus.visible = false;
				btnStart.visible = false;
			}
			else
			{
				txtTime.text = "（" + timeSlider.value / 10 + " 小时）";
				txtMoney.text = timeSlider.value * 6 * skillRank + " 钱";
				btnTimeMinus.visible = true;
				
				if (txtSkill.text != "" && txtTime.text != "")
				{
					btnStart.visible = true;
				}
			}
			
			if ((timeSlider.value) / 10 < lastTime && (timeSlider.value + 10) <= (roleEnergy + roleEnergyAdd) && ((timeSlider.value + 10) * 6 * skillRank <= roleMoney))
			{
				btnTimePlus.visible = true;
			}
			else
			{
				btnTimePlus.visible = false;
			}
		}
		
		private function onSelected(evt:ListEvent):void
		{
			skillID = evt.item["SkillID"];
			skillRank = evt.item["等级"];
			teacherID = evt.item["RoleID"];
			lastTime = evt.item["剩余小时"];
			
			txtSkill.text = evt.item["技能"];
			
			reRoad();
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "Command-23"]));
					break;
				case "btnStart":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["CommandCall(23,CommandUse," + targetID + ",skillId=" + skillID + "&teacherId=" + teacherID + "&actTime=" + timeSlider.value * 6 + ");"]]));
					break;
				case "btnTimeMinus":
					timeSlider.value -= 10;
					reRoad();
					break;
				case "btnTimePlus":
					timeSlider.value += 10;
					reRoad();
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnStart.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}